<?php
/**
 * This file contains the config class.
 *
 * PHP version 5
 *
 * @category  PHP
 * @package   PHPMY
 * @author    Sven Ruttloff, www.sruttloff.de <service@sruttloff.de>
 * @copyright 2010 Sven Ruttloff, www.sruttloff.de
 * @license   http://www.sruttloff.de/cms/files/licence_en_lgpl.txt LGPL
 * @version   SVN: $Id: config.class.php 126 2010-10-11 09:28:28Z sruttloff@googlemail.com $
 * @link      phpmy
 */

/**
 * Configuration parameter management
 *
 * @category  PHP
 * @package   PHPMY
 * @author    Sven Ruttloff, www.sruttloff.de <service@sruttloff.de>
 * @copyright 2010 Sven Ruttloff, www.sruttloff.de
 * @license   http://www.sruttloff.de/cms/files/licence_en_lgpl.txt LGPL
 * @link      phpmy
 */
class config
{
    /**
     * @var	array	assoc. key => value
     */
    protected $c_array = array ();
    /**
     * @var mixed if not !== true, something is going wrong
     */
    protected $STATUS = true;
    /**
     * @var string dir of config file
     */
    protected $path = null;
    protected $filename = null;
    protected $filename_ext = '.cfg.php';
    /**
     * @var object instance
     */
    protected static $INSTANCE = array();
    /**
     * Load config file into array
     *
     * @param string $s_file filename
     *
     * @return bool
     */
    protected function __construct($s_file)
    {
        $this->path = WRITEABLEDIR . '/config/';
        if ($s_file)
            $this->filename = $s_file . $this->filename_ext;
        $this->load();
    }
    /**
     * Load config file into array
     *
     * @return bool
     */
    protected function load()
    {
        if (file_exists($this->path . $this->filename))
        {
            if (!$farr = file($this->path . $this->filename))
                $this->STATUS = "couldn't open config file for reading: " . $this->filename;
            reset($farr);
            while (list ($k, $v) = each($farr))
            {
                if (substr($v, 0, 1) != '#')
                {
                    $v = substr($v, 0, strlen($v) - 1);
                    $cfarr = explode('::', $v);
                    if (isset($cfarr[1]))
                        $this->c_array[$cfarr[0]] = trim($cfarr[1]);
                }
            }
        } else return false;
        return true;
    }
    /**
     * return a instance of object
     *
     * @param string $s_file name and instance of config file, default = config
     *
     * @return config instance
     */
    public static function getInstance($s_file = 'config')
    {
        if (!isset(self::$INSTANCE[$s_file]))
            self::$INSTANCE[$s_file] = new self($s_file);
        return self::$INSTANCE[$s_file];
    }
    /**
     * return the status of object
     *
     * @return bool|string true or text with failure message
     */
    public function getStatus()
    {
        return $this->STATUS;
    }
    /**
     * returns a config value
     *
     * @param string $key config item name
     *
     * @return string config item value
     */
    public function get($key)
    {
        return isset ($this->c_array[strtolower($key)]) ? $this->c_array[strtolower($key)] : false;
    }
    /**
     * save the content from array into config file and reload object
     *
     * @param string $skip some item to skip
     *
     * @return bool
     */
    public function save($skip = '')
    {
        $frontend = frontend::getInstance();
        // check if config dir exists
        if (is_object($frontend))
        {
            if (!is_dir($this->path))
                if (!createDir($this->path, $frontend->getConfig()->Get('chmod_dir')))
                    trigger_error('Can not create config directory', E_USER_ERROR);
        }
        @reset($this->c_array);
        $ttl = (string) null;
        while (list ($k, $v) = @ each($this->c_array))
        {
            if (strtolower($skip) != $k)
                $ttl .= "$k::$v\n";
        }
        $ttl = "#<?php/*\n" . $ttl . "#*/?>";
        $chmod = $frontend->getConfig()->Get('chmod_files');
        if (!createFile($this->path . $this->filename, $ttl, ($chmod ? $chmod : false)))
        {
            $this->STATUS = "couldn't create config file: " . $this->path . $this->filename;
            trigger_error($this->STATUS, E_USER_ERROR);
            return false;
        }
        if ($skip)
        {
            unset ($this->c_array);
            return $this->__construct();
        }
        return true;
    }
    /**
     * set a config item with a value
     *
     * @param string $key   item name
     * @param string $value item value
     *
     * @return bool
     */
    public function set($key, $value)
    {
        $key = strtolower($key);
        $this->c_array[$key] = $value;
        return $this->save();
    }
    /**
     * return array data
     *
     * @return array key, value pair
     */
    public function getAllKeyValue()
    {
        return $this->c_array;
    }
    /**
     * fetch array data
     *
     * @return array key, value pair
     */
    public function fetch()
    {
        return each($this->c_array);
    }
    /**
     * reset array iterator
     *
     * @return void
     */
    public function fetchReset()
    {
        reset($this->c_array);
    }
    /**
     * return quantity of elements
     *
     * @return int
     */
    public function modCount()
    {
        return count($this->c_array);
    }
    /**
     * reinit config
     *
     * @return void
     */
    public function reInit ()
    {
        $this->load();
    }
}